package org.huntaway.dom4j;

import java.io.File;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.huntaway.dom4j.po.FileInfoPo;
import org.huntaway.dom4j.po.FilePo;
import org.huntaway.kernel.TimesTools;
import org.huntaway.kernel.imp.CurrToolsImp;
import org.huntaway.kernel.imp.TimesToolsImp;

public class Dom4jTools  extends CurrToolsImp{
	TimesTools timesTools = new TimesToolsImp();

	public void updateElement(Element rootEle, File file) {
		Element fileEle = rootEle.addElement(FilePo.file);
		fileEle.addElement(FilePo.filename).setText(file.getName());
		fileEle.addElement(FilePo.srcPath).setText(file.getPath());
		fileEle.addElement(FilePo.lastModified).setText(
				"" + file.lastModified());
		fileEle.addElement(FilePo.fileSize).setText(getSize(file));
	}

	public void insertFile(Element root, FileInfoPo filePo) {
		Element eleFile = root.addElement(filePo.file);
		eleFile.addAttribute(filePo.code, filePo.getCode());
		eleFile.addAttribute(filePo.name, filePo.getName());
		eleFile.addAttribute(filePo.filename, filePo.getFilename());
		Element srcEle = eleFile.addElement(filePo.srcPath);
		srcEle.addAttribute(filePo.num, "1");
		{
			Element fileItemEle = srcEle.addElement(filePo.prefix + "1");
			fileItemEle.addAttribute(filePo.fileSize, filePo.getFileSize());
			fileItemEle.addAttribute(filePo.lastModified,
					filePo.getLastModified());
			fileItemEle.addAttribute(filePo.downloadUrl,
					filePo.getDownloadUrl());
			fileItemEle.addAttribute(filePo.version, filePo.getVersion());

			fileItemEle.addAttribute(filePo.distPath, filePo.getDistPath());
			fileItemEle.setText(filePo.getSrcPath());
		}
		eleFile.addElement(filePo.tags).setText(filePo.getTags());
		eleFile.addElement(filePo.description).setText(filePo.getDescription());
		eleFile.addElement(filePo.typeId).setText(filePo.getTypeId());
	}

	private String getSize(File temFile) {
		return FormetFileSize(temFile.length()) + "(" + temFile.length() + ")";
	}

	public String FormetFileSize(long fileS) {// 转换文件大小
		DecimalFormat df = new DecimalFormat("#.00");
		String fileSizeString = "";
		if (fileS < 1024) {
			fileSizeString = df.format((double) fileS) + "B";
		} else if (fileS < 1048576) {
			fileSizeString = df.format((double) fileS / 1024) + "K";
		} else if (fileS < 1073741824) {
			fileSizeString = df.format((double) fileS / 1048576) + "M";
		} else {
			fileSizeString = df.format((double) fileS / 1073741824) + "G";
		}
		return fileSizeString;
	}
	/**
	 * @param parMap
	 * @return
	 */
	public Map writerDocument(Map parMap) {
		Map resmap = new HashMap();
		try {
			/** 将document中的内容写入文件中 */
			Document doc = (Document) parMap.get("doc");
			String file = (String) parMap.get("file");
			String encode = (String) parMap.get("encode");
			OutputFormat format = OutputFormat.createPrettyPrint();
			if (encode == null || encode.equals("")) {
				format.setEncoding(DEFAULT_CHATSET);
			} else {
				format.setEncoding(encode);
			}
			XMLWriter writer = new XMLWriter(new FileWriter(new File(file)),
					format);
			writer.write(doc);
			writer.close();
			resmap.put("state", new Boolean(true));
		} catch (Exception ex) {
			resmap.put("state", new Boolean(false));
			resmap.put("ex", ex);
			ex.printStackTrace();
		}
		return resmap;
	
	}
}
